//
//  UserListViewController.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/5/6.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "UserListViewController.h"
#import "DZNSegmentedControl.h"
#import "AccountTableViewCell.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "DetailsWebViewController.h"
#import "NewLineListCell.h"
#import "DataImageViewController.h"
#import "MainTabBarController.h"
#import "ListViewController.h"
#import "LineView.h"
#import "NewLineListCell.h"
#import "ListView.h"
#import "NoPaymentCell.h"
#import "DateBut.h"
#import "SelectButton.h"
#import "MyListModel.h"
#import "BassViewController.h"
#import "PMCalendar.h"
#import "GMDCircleLoader.h"
#import "YiFaHuoCell.h"

#import "SDRefresh.h"


#import "Reachability.h"
#import "NetStatus.h"

@interface UserListViewController ()<MyListModelDelegate,UITextFieldDelegate,PMCalendarControllerDelegate,NoPaymentCelDelegate,UIGestureRecognizerDelegate>
{

    UILabel *_stateLabel;//订单状态
    UIButton *_dateBut;
    NSInteger selete;

    UIButton *_yiFuKuan;//已付款
    UIButton *_weiFuKuanBut;//未付款but
    UIButton *_searchImage;//搜索图标
    
    //-------------未下拉框定义的变量------------------
    UITableView *_selectTableView;//下拉框
    SelectButton *_selectBut;
    UIView *_aview;
    NSArray *_selectArr;
    //-----------------------------------------
    
    NSInteger page;
    //搜索框
    UITextField *_searchView;
    BOOL isClick;
    BOOL _isYifahuo;
    UITapGestureRecognizer *_singleTap;
    
}

@property (nonatomic, strong) SDRefreshFooterView *refreshFooter;

@property (nonatomic, strong) DateBut *mydateBtn;//时间but
@property (nonatomic, strong) MyListModel *myListModel;//Model
@property (nonatomic, strong) NSMutableArray *myLiatArray;//Model

@property (nonatomic, copy) NSString *startTime;//开始时间
@property (nonatomic, copy) NSString *endTime;//结束时
@property (nonatomic, strong) PMCalendarController *pmCC;
@end

@implementation UserListViewController

- (void)dealloc{
    [self.view removeGestureRecognizer:_singleTap];
     [self.refreshFooter removeFromSuperview];
}
-(instancetype)init{
    self = [super init];
    if (self) {
        _selectArr = @[@"未发货",@"已发货",@"已签收"];
        self.myLiatArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    selete = 0;
        [self.navigationItem setHidesBackButton:YES];
    _isYifahuo = NO;//默认
    
    
    [self createNavSubviews];
    [self createTableView];
    [self createDateButton];
    isClick = YES;
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    _singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    _singleTap.delegate = self;
    [self.view addGestureRecognizer:_singleTap];
    
    [self setupFooter];
    
    //判断是否有网
    NetStatus *status = [[NetStatus alloc] init];
    if ([status.networkStatus isEqualToString:@"无网络"]) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    page = 1;
    //获取已付款的订单
    self.myListModel = [[MyListModel alloc] init];
    self.myListModel.myListModelDelegate = self;
    NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
    NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2"};
    
    [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListYiFuKuanInterface];
}


- (void)setupFooter
{
    self.refreshFooter = [SDRefreshFooterView refreshView];
    [self.refreshFooter addToScrollView:self.tableView];
    [self.refreshFooter addTarget:self refreshAction:@selector(footerRefreshs)];
}



- (void)footerRefreshs
{
       [self.refreshFooter endRefreshing];
//    if (self.myLiatArray.count % 10 != 0) {
//        [self.refreshFooter endRefreshing];
//        return;
//    }
//    NSInteger page = self.myLiatArray.count / 10;
//    page += 1;
//    NSString *pageNum = [NSString stringWithFormat:@"%ld",page];
//    NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
//    
//    if (selete == 0) {
//        //已付款上拉加载
//        NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"PageID":pageNum};
//        [self.myListModel getaddUserList:self.myLiatArray parameter:parameterDic interface:UserListYiFuKuanInterface];
//    } else{
//        //未付款上拉加载
//         NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"PageID":pageNum};
//        [self.myListModel getaddUserList:self.myLiatArray parameter:parameterDic interface:UserListWeiFuKuanInterface];
//      
//    }
    
}



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    if ([touch.view isKindOfClass:[UIButton class]] || [touch.view isKindOfClass:[cell.contentView class]]){
        
        return NO;
        
    }
    
    return YES;
    
}

#pragma -mark 点击self.view的点击手势
-(void)handleSingleTap:(UITapGestureRecognizer *)sender
{
    
//    [self.view endEditing:YES];
    [_searchView resignFirstResponder];
}
#pragma -mark 创建TabView方法
- (void)createTableView{
    _aview = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40)];
    _aview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_aview];
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 16, 10, 10)];
    image.image = [UIImage imageNamed:@"自动补货订单---订单详情_03"];
    [_aview addSubview:image];
    
    //库存订单View
    UILabel *kuCunTitle = [[UILabel alloc] initWithFrame:CGRectMake(image.right+5, 5, 80, 30)];
    kuCunTitle.text = @"客户订单-";
    [_aview addSubview:kuCunTitle];
    
    _stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(kuCunTitle.right, 5, 100, 30)];
    _stateLabel.text = @"已付款";
    [_aview addSubview:_stateLabel];
    
    
    _selectBut = [[SelectButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70, 5, 60, 30)];
     [_selectBut setBackgroundImage:[UIImage imageNamed:@"下线管理_151"] forState:UIControlStateNormal];
    _selectBut.selectTitle.text =@"全部";
    [_selectBut addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [_aview addSubview:_selectBut];
    
    
    UIView*line = [[UIView alloc] initWithFrame:CGRectMake(0, _aview.bottom+1, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = DaoHangColor;
    [self.view addSubview:line];
    
    UILabel*lineview = [[UILabel alloc] initWithFrame:CGRectMake(0, line.bottom, SCREEN_WIDTH, 10)];
    lineview.backgroundColor = LineColor;
    [self.view addSubview:lineview];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, lineview.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-_aview.frame.size.height-10-1.5-100)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    //返回按钮
    
    UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50)];
    backview.backgroundColor = [UIColor grayColor];

    [self.view addSubview:backview];
    
    //已付款
     _yiFuKuan = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH-0.5)/2.0, 50)];
    [_yiFuKuan setBackgroundImage:[UIImage imageNamed:@"客户订单_已付款1"] forState:UIControlStateNormal];
    [_yiFuKuan addTarget:self action:@selector(yifukuan:) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:_yiFuKuan];
    
    
    //未付款
     _weiFuKuanBut = [[UIButton alloc] initWithFrame:CGRectMake(_yiFuKuan.right+0.5, 0, (SCREEN_WIDTH-0.5)/2.0, 50)];
    [_weiFuKuanBut addTarget:self action:@selector(weiFhKuan:) forControlEvents:UIControlEventTouchUpInside];
    [_weiFuKuanBut setBackgroundImage:[UIImage imageNamed:@"客户订单_未付款1"] forState:UIControlStateNormal];
    [backview addSubview:_weiFuKuanBut];
    
    
    //下拉弹出TableView
    _selectTableView = [[UITableView alloc] initWithFrame:CGRectMake(_selectBut.frame.origin.x, _aview.bottom-5, _selectBut.frame.size.width, 0)];
    _selectTableView.delegate = self;
    _selectTableView.bounces = NO;
    _selectTableView.dataSource = self;
    [_selectTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];

    
    [self.view addSubview:_selectTableView];
    
    
    
}
#pragma -mark 点击键盘return代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if ([_searchView.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入要搜索的订单号/用户名/电话" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return YES;
    }
    if (selete == 0) {
        NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
        NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"Search":_searchView.text};
        [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListYiFuKuanInterface];
    } else{
        NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
        NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"Search":_searchView.text};
        [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListWeiFuKuanInterface];
    }
    return YES;
}

#pragma -mark 选择按钮点击事件
-(void)selectButtonClick:(SelectButton *)sender{
    //sender.selected = !sender.selected;
//    if (sender.selected) {
//        [self showSelectTableView];
//    }else
//        [self disappearSelectTableView];
    if (isClick) {
        [self showSelectTableView];
        isClick = NO;
    }else{
        [self disappearSelectTableView];
        isClick = YES;
    }
}

#pragma -mark textField 的代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
          textField.keyboardType = UIKeyboardTypeNumberPad;
    return YES;
}
#pragma -mark 弹出的TableView选择框事件
- (void)showSelectTableView{
//    [UIView beginAnimations:@"imageViewBig" context:nil];
//    [UIView setAnimationDuration:1];
    _selectTableView.frame = CGRectMake(_selectBut.frame.origin.x, _aview.bottom-5, _selectBut.frame.size.width, 130);
//    [UIView commitAnimations];
}
#pragma -mark 收起弹出的TableView事件
- (void)disappearSelectTableView{
//    [UIView beginAnimations:@"imageViewBig" context:nil];
//    [UIView setAnimationDuration:1];
    _selectTableView.frame = CGRectMake(_selectBut.frame.origin.x, _aview.bottom-5, _selectBut.frame.size.width, 0);
//    [UIView commitAnimations];
}

#pragma -mark 创建日期Button方法
- (void)createDateButton{
    
    
    self.mydateBtn = [DateBut dateButWithButton];

    [self.mydateBtn addTarget:self action:@selector(dateBtnActionclick:) forControlEvents:UIControlEventTouchUpInside];
    
//    UIBarButtonItem *dateButItem = [[UIBarButtonItem alloc] initWithCustomView:self.mydateBtn2];
//    self.navigationItem.rightBarButtonItem = dateButItem;
    
    UIWindow *windows = [[[UIApplication sharedApplication] delegate ] window];
    self.mydateBtn.frame = CGRectMake(SCREEN_WIDTH-70, 30, 60, 25);
    [windows addSubview:self.mydateBtn];

}
#pragma -mark 获取日期
- (void)dateBtnActionclick:(UIButton *)sender{
    NSLog(@"日期");
    self.pmCC = [[PMCalendarController alloc] init];
    self.pmCC.delegate = self;
    self.pmCC.mondayFirstDayOfWeek = YES;
    
    [self.pmCC presentCalendarFromView:sender permittedArrowDirections:PMCalendarArrowDirectionAny animated:YES];
    
    [self calendarController:self.pmCC didChangePeriod:self.pmCC.period];
    
}

static int i = 0;
- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod
{
    NSString *time =[NSString stringWithFormat:@"%@"
                     , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM"]];
    [self.mydateBtn setTitle:time forState:UIControlStateNormal];
    
    self.startTime = [NSString stringWithFormat:@"%@%@", [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-"],@"1"];
    NSInteger mydate = [self gainMonthNumber];
    self.endTime = [NSString stringWithFormat:@"%@%zi", [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-"],mydate];
    NSLog(@"开始时间 ＝＝＝ %@,结束时间 ＝＝ %@",_startTime,_endTime);
    
    if (i%2!=0) {
        [self.pmCC dismissCalendarAnimated:YES];
        
    }
    
    i++;
}

#pragma -mark 获取当月天数
-(NSInteger)gainMonthNumber{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //    NSDate *date = [dateFormatter dateFromString:@"2015-02-04 16:01:03"];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
    
    NSUInteger numberOfDaysInMonth = range.length;
    return  numberOfDaysInMonth;
}



- (BOOL)calendarControllerShouldDismissCalendar:(PMCalendarController *)calendarController{


    return YES;
}

- (void)calendarControllerDidDismissCalendar:(PMCalendarController *)calendarController{

    //选择日期结束
        [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    
    //判断是否有网
    NetStatus *status = [[NetStatus alloc] init];
    if ([status.networkStatus isEqualToString:@"无网络"]) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (selete == 0) {
        NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
        NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"StartTime":self.startTime,@"EndTime":self.endTime};
        [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListYiFuKuanInterface];
    } else{
        NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
        NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"StartTime":self.startTime,@"EndTime":self.endTime};
        [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListWeiFuKuanInterface];
    }
    
}

#pragma -mark 掉用支付返回结果
- (void)paymentZhiFuEnd{
    
   [GMDCircleLoader hideFromView:self.view animated:YES];
    [self weiFhKuan:nil];
}
#pragma -makr 点击来支付按钮
- (void)touchzhifu{
    
    [GMDCircleLoader setOnView:self.view withTitle:@"支付中..." animated:YES];
}
#pragma -mark 支付错误代理
- (void)zhifuError{
 [GMDCircleLoader hideFromView:self.view animated:YES];
}
#pragma -mark 获取客户订单数据结束
- (void)getUserlistEnd{
    [self.refreshFooter endRefreshing];
    _yiFuKuan.userInteractionEnabled = YES;
    _weiFuKuanBut.userInteractionEnabled = YES;
    [GMDCircleLoader hideFromView:self.view animated:YES];
    _searchImage.userInteractionEnabled = YES;
    NSLog(@"获取客户订单完成==== %@",self.myLiatArray);
    [self.tableView reloadData];
}
#pragma -mark 已付款点击事件
- (void)yifukuan:(UIButton *)sender{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    sender.userInteractionEnabled = NO;
    _stateLabel.text = @"已付款";
    _selectBut.hidden = NO;
    _selectTableView.hidden = NO;
    selete = 0;
    [_yiFuKuan setBackgroundImage:[UIImage imageNamed:@"客户订单_已付款1"] forState:UIControlStateNormal];
    [_weiFuKuanBut setBackgroundImage:[UIImage imageNamed:@"客户订单_未付款1"] forState:UIControlStateNormal];
    NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
    NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2"};
    
    
    //判断是否有网
    NetStatus *status = [[NetStatus alloc] init];
    if ([status.networkStatus isEqualToString:@"无网络"]) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListYiFuKuanInterface];
}

#pragma -mark 未付款点击事件
- (void)weiFhKuan:(UIButton *)sender{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    sender.userInteractionEnabled = NO;
    _stateLabel.text = @"未付款";
    _selectBut.hidden = YES;
     _selectTableView.hidden = YES;
    selete = 1;
    [_weiFuKuanBut setBackgroundImage:[UIImage imageNamed:@"客户订单_未付款2"] forState:UIControlStateNormal];
    [_yiFuKuan setBackgroundImage:[UIImage imageNamed:@"客户订单_已付款2"] forState:UIControlStateNormal];
    
    //判断是否有网
    NetStatus *status = [[NetStatus alloc] init];
    if ([status.networkStatus isEqualToString:@"无网络"]) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
    NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2"};
    [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListWeiFuKuanInterface];
}

#pragma -mark 向服务器请求返还的网络异常时喝错误时
- (void)requestError{
    [self.refreshFooter endRefreshing];
    [GMDCircleLoader hideFromView:self.view animated:YES];
    _yiFuKuan.userInteractionEnabled = YES;
    _weiFuKuanBut.userInteractionEnabled = YES;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    
}
- (void)getrequesterror:(NSString *)error{
    [self.refreshFooter endRefreshing];
    _yiFuKuan.userInteractionEnabled = YES;
    _weiFuKuanBut.userInteractionEnabled = YES;
      [self.tableView reloadData];
    [GMDCircleLoader hideFromView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:error delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    
}
#pragma -mark 创建导航上的搜索框／返回按钮的方法
- (void)createNavSubviews{

    
    
    UIView *qview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    
    UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    backBut.frame = CGRectMake(-25, -2, 85, 44);
    
    [backBut setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [qview addSubview:backBut];
    
//    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-110, 5, 200, 30)];
//    title.textAlignment = NSTextAlignmentCenter;
//    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
//    title.text = @"订单";
//    title.textColor = [UIColor whiteColor];
//    [qview addSubview:title];
    self.navigationItem.titleView = qview;
    
    //搜索框
    _searchView = [[UITextField alloc]initWithFrame:CGRectMake(backBut.right, 5, SCREEN_WIDTH-backBut.frame.size.width*2, 30)];
    _searchView.layer.masksToBounds = YES;
    _searchView.delegate = self;
    _searchView.layer.cornerRadius = 6/2;
    _searchView.textColor = [UIColor whiteColor];
    _searchView.backgroundColor = SearColor;
    _searchView.placeholder = @"输入姓名/电话/订单号";
    [_searchView setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    _searchView.font = [UIFont fontWithName:nil size:12];
    [qview addSubview:_searchView];
    
    //搜索图片
    
    _searchImage = [[UIButton alloc]initWithFrame:CGRectMake(_searchView.right-30, 5, 30, 30)];
    _searchImage.backgroundColor = [UIColor clearColor];
    [_searchImage setImage:[UIImage imageNamed:@"订单--我的订单_03_02"] forState:UIControlStateNormal];
    [_searchImage addTarget:self action:@selector(searchClick:) forControlEvents:UIControlEventTouchUpInside];
    [qview addSubview:_searchImage];
//    

    
    
}

#pragma -mark 搜索事件
- (void)searchClick:(UIButton *)sender{
    [_searchView resignFirstResponder];
    if ([_searchView.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入要搜索姓名/电话/订单号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    //判断是否有网
    NetStatus *status = [[NetStatus alloc] init];
    if ([status.networkStatus isEqualToString:@"无网络"]) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
        [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    if (selete == 0) {
        
        NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
        NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"Search":_searchView.text};
        [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListYiFuKuanInterface];
    } else{
        NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
        NSDictionary *parameterDic = @{@"AgentID":agentId,@"OrderTypeID":@"2",@"Search":_searchView.text};
        [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListWeiFuKuanInterface];
    }
    
}


#pragma -mark 返回按钮的点击事件
- (void)backClick{
    [self.mydateBtn removeFromSuperview];
    self.listVC.hidesBottomBarWhenPushed = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == _selectTableView) {
        return [_selectArr count];
    } else
    return self.myLiatArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == _selectTableView) {
        return 31;
    }else{
    
        NSDictionary *dic = self.myLiatArray[indexPath.row];
        MyListModel *listModel = dic[@"YiFuKuanModel"];
        if ([listModel.MasterOrderStatus isEqualToString:@"已发货"]){
            
            return 295;
        }
        else
            return 286;
    }
   
 
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    //------------------------下拉弹出TableVe的Cell----------------------------------------
    
    static NSString *selectCellident = @"selectCell";
    UITableViewCell *seleceCell = [tableView dequeueReusableCellWithIdentifier:selectCellident];
    UILabel *selectLibel;
    if (seleceCell == nil) {
        seleceCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:selectCellident];
        seleceCell.selectionStyle  =UITableViewCellSelectionStyleNone;
        selectLibel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, seleceCell.frame.size.width, seleceCell.frame.size.height)];
        selectLibel.font = [UIFont fontWithName:nil size:13];
        selectLibel.textColor = [UIColor whiteColor];
        [seleceCell addSubview:selectLibel];
        seleceCell.backgroundColor = [self colorWithHexString:@"#5D5D5D"];;
    }
    if (tableView == _selectTableView) {
        selectLibel.text = _selectArr[indexPath.row];
        return seleceCell;
    }
    
    /**********
                 -------------------以下是客户订单页主要显示的Cell-----------------------------
     **********/
    NSDictionary *dic = self.myLiatArray[indexPath.row];
    NSArray *modelarray =dic[@"ProductInfoModelArr"];
    
    static NSString *accountCell = @"Cell";
    static NSString *nopayment = @"nopaymentCell";
    static NSString *yiFaHuo = @"yifahuo";
    MyListModel *listModel = dic[@"YiFuKuanModel"];
    if (selete == 0) {
        YiFaHuoCell *yifahuoCell  = [tableView dequeueReusableCellWithIdentifier:yiFaHuo];
        if (yifahuoCell == nil) {
            yifahuoCell = [[YiFaHuoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:yiFaHuo];
        }
        if ([listModel.MasterOrderStatus isEqualToString:@"已发货"]) {
            [yifahuoCell showUserTableCellViewData:listModel arr:modelarray];
            return yifahuoCell;
        } else {
            NewLineListCell *cell = [tableView dequeueReusableCellWithIdentifier:accountCell];
            if (cell == nil) {
                cell = [[NewLineListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:accountCell];
            }
            [cell showUserTableCellViewData:listModel arr:modelarray];
            cell.lineView.queRenBut.hidden = YES;
            return cell;
        }
    }
    else{
        NoPaymentCell   *nopaymentcell = [tableView dequeueReusableCellWithIdentifier:nopayment];
        if (nopaymentcell == nil) {
            nopaymentcell = [[NoPaymentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nopayment];
            nopaymentcell.noPaymentCelDelegate = self;
        }
        [nopaymentcell showUserTableCellViewData:listModel arr:modelarray];
        return nopaymentcell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _selectTableView) {
        _selectBut.selectTitle.text = _selectArr[indexPath.row];
        isClick = YES;
        _selectTableView.frame = CGRectMake(_selectBut.frame.origin.x, _aview.bottom-5, _selectBut.frame.size.width, 0);
        NSString *OrderID;
        switch (indexPath.row) {
            case 0:
                OrderID = @"2";
                _isYifahuo = NO;
                break;
            case 1:
                 OrderID = @"3";
                _isYifahuo = YES;
                break;
            default:
                _isYifahuo = NO;
                 OrderID = @"4";
                break;
        }
        self.myLiatArray = [[NSMutableArray alloc]init];
        NSDictionary *parameterDic = @{@"AgentID":[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"],@"OrderTypeID":OrderID};
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
        //判断是否有网
        NetStatus *status = [[NetStatus alloc] init];
        if ([status.networkStatus isEqualToString:@"无网络"]) {
            [GMDCircleLoader hideFromView:self.view animated:YES];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
        [self.myListModel getUserList:self.myLiatArray parameter:parameterDic interface:UserListYiFuKuanInterface];
        [self.tableView reloadData];
    }
}

//传入字符串返回RGB
- (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
    
}
@end
